package com.xiashitech.agent.instrumentation.base.context;

import com.google.errorprone.annotations.MustBeClosed;
import io.opentelemetry.javaagent.shaded.io.opentelemetry.context.ContextKey;
import javax.annotation.Nullable;


public interface XSContext {

    static XSContext current() {
        XSContext current = XSContextStorage.get().current();
        return current != null ? current : root();
    }

    static XSContext root() {
        return XSContextStorage.get().root();
    }

    @Nullable
    <V> V get(ContextKey<V> key);

    <V> XSContext with(ContextKey<V> k1, V v1);

    default XSContext with(XSImplicitContextKeyed value) {
        return value.storeInContext(this);
    }

    default XSContext withForConfluence(XSImplicitContextKeyed value) {
        return value.storeInContextForConfluence(this);
    }

    @MustBeClosed
    default void makeCurrent() {
        XSContextStorage.get().attach(this);
    }
}